package com.kingyea.gradle.task;

import java.io.IOException;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

import com.kingyea.gradle.constant.ConstantValues;
import com.kingyea.gradle.extension.Deployer;
import com.kingyea.gradle.extension.RemoteLinuxDeploy;
import com.kingyea.gradle.jsch.JschSession;
import com.kingyea.gradle.jsch.JschSessionFactory;
import com.kingyea.gradle.jsch.SessionConfiguration;
import com.kingyea.gradle.jsch.SftpChannel;
import com.kingyea.gradle.jsch.UploadSftpProgressMonitor;
import com.kingyea.gradle.jsch.command.CommandExecutor;
import com.kingyea.gradle.jsch.command.ExecCommandExecutor;
import com.kingyea.gradle.jsch.command.ExecuteResult;

public class RemoteDeployTask extends DefaultTask {
	
	@TaskAction
	public void remoteDeploy() throws Exception {
		Deployer deployer = this.getProject().getExtensions().getByType(Deployer.class);
		//远程发布只针对linux操作系统
		if(ConstantValues.OS_LINUX.equals(deployer.getTargetOs())) {
			JschSession jschSession = null;
			CommandExecutor commandExecutor = null;
			SftpChannel sftpChannel = null;
			try {
				RemoteLinuxDeploy linuxDeploy = this.getProject().getExtensions().getByType(RemoteLinuxDeploy.class);
				SessionConfiguration configuration = SessionConfiguration.parse(linuxDeploy);
				JschSessionFactory factory = new JschSessionFactory(configuration);
				System.out.println("start connecting to " + linuxDeploy.getHost());
				jschSession = factory.openSession();
				commandExecutor = new ExecCommandExecutor(jschSession);
				String beforeDeployCommand = linuxDeploy.getBeforeDeployCommand();
				
				System.out.println("remote deployment：" + linuxDeploy.getHost());
			    System.out.println("deployment path:" + linuxDeploy.getDeployPath());
				
			    
			    
			    if(beforeDeployCommand!=null && !beforeDeployCommand.isEmpty()) {
			    	System.out.println("executing command: " + beforeDeployCommand);
			    	ExecuteResult result = commandExecutor.execute(beforeDeployCommand);
			    	if(result!=null) {
			    		System.out.println(result.getResult());
			    	}
			    }
				
				System.out.println("start uploading files....");
				sftpChannel = jschSession.getSftpChannel();
				sftpChannel.open();
				sftpChannel.uploadDir(deployer.getDeployPath(), linuxDeploy.getDeployPath(), new UploadSftpProgressMonitor());
				
				String afterDeployCommand = linuxDeploy.getAfterDeployCommand();
				if(afterDeployCommand!=null && !afterDeployCommand.isEmpty()) {
					System.out.println("executing command: " + afterDeployCommand);
					ExecuteResult result = commandExecutor.execute(afterDeployCommand);
					if(result!=null) {
			    		System.out.println(result.getResult());
			    	}
				}
				System.out.println("remote deployment complete.");
			} finally {
				release(jschSession, commandExecutor, sftpChannel);
			}
			
		}
	}

	private void release(JschSession jschSession, CommandExecutor commandExecutor, SftpChannel sftpChannel)	throws IOException {
		if(sftpChannel!=null) {
			sftpChannel.close();
		}
		if(jschSession!=null) {
			jschSession.close();
		}
		if(commandExecutor!=null) {
			commandExecutor.close();
		}
	}
}
